package com.mindao.app.ued.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mindao.app.common.spring.controller.BaseBaseController;
import com.mindao.app.common.spring.model.BaseModel;
import com.mindao.app.common.spring.service.BaseService;
import com.mindao.app.common.spring.service.util.JsonResult;
import com.mindao.app.ued.model.UserDemo;
import com.mindao.utils.json.JsonUtil;

/**
/* 版权所有： 广州敏道科技有限公司
/*
/* 功能描述：
/*
/* 创 建 人：李国才
/* 创建时间：2016年7月12日 下午9:07:11 	
 **/
@Controller
 
@RequestMapping("/ued/userdemoangular")
public class UserDemoAngularController  extends BaseBaseController  {
	
	private final Logger log = LoggerFactory.getLogger(UserDemoAngularController.class);
	
	@javax.annotation.Resource(name = "baseService")
	private BaseService baseService;

	
	@RequestMapping
	public String manage(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs ){
		return super.rtnDefault(request, response, session, rhs);
	}
	
	/**
	 * 新增或修改用户
	 * @param request
	 * @param response
	 * @param session
	 * @param rhs
	 * @param user
	 * @return
	 */
	//默认情况下，jQuery传输数据使用Content-Type: x-www-form-urlencodedand和类似于"foo=bar&baz=moe"的序列，然而AngularJS，传输数据使用Content-Type: application/json和{ "foo": "bar", "baz": "moe" }这样的json序列
	//所以方案1：前台把Content-Type设置成x-www-form-urlencodedand之后，还需要转换序列的格式
	//所以方案2：后台增加参数增加@RequestBody
	@ResponseBody
	@RequestMapping(method=RequestMethod.POST)
	public JsonResult submitData(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs,  UserDemo user){//@RequestBody
		//String addFlag =request.getParameter("addFlag");
		if (user.getId()==null){
			baseService.create(user);
			JsonResult jr=new JsonResult(JsonResult.SUCCESS,"");
			return jr;
		}else{
			UserDemo ud=(UserDemo)baseService.loadById(UserDemo.class, Long.valueOf(user.getId()));
			ud.setName(user.getName());
			ud.setAge(user.getAge());
			baseService.updateDirectly(ud);
			JsonResult jr=new JsonResult(JsonResult.SUCCESS,"");
			return jr;
		}
	}
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @param session
	 * @param rhs
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(method=RequestMethod.POST)
	public JsonResult deleteUser(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs,  UserDemo user){//@RequestBody
 
		baseService.delete(user);
		JsonResult jr=new JsonResult(JsonResult.SUCCESS,"");
		return jr;
		//return super.rtnDefault(request, response, session, rhs);
	}
 
	
    /**
     * 加载所有用户
     * @param request
     * @param response
     * @param session
     * @param rhs
     */
	@ResponseBody
	@RequestMapping(method=RequestMethod.GET)
	public JsonResult getAllUsers(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs ){
		List  list= baseService.find("from UserDemo where status>?",BaseModel.STATUS_REMOVED);
		try {
			List jsonList=JsonUtil.convertBeanToMap(list, true,new String[]{"id","name","age"});
			response.getWriter().println(JSON.toJSONString(jsonList));
			return null;
			//JsonResult jr=new JsonResult(JsonResult.SUCCESS,"",jsonList);
			//return jr;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
